package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.PersonExperienceMapper;
import com.zhijian.medical.entity.po.PersonExperience;
import com.zhijian.medical.entity.vo.request.PersonExperienceSearchVo;
import com.zhijian.medical.entity.vo.response.PersonExperienceVo;
import com.zhijian.medical.service.PersonExperienceService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员学习或工作经历业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-23 22:25:40
 */
@Slf4j
@Service
public class PersonExperienceServiceImpl implements PersonExperienceService {

    @Autowired
    private PersonExperienceMapper mapper;

    @Override
    public PersonExperience selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonExperienceVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByPersonId(Long personId) {
        mapper.deleteByPersonId(personId);
    }

    @Override
    public Long insert(PersonExperience personExperience) {
        Date now = new Date();
        personExperience.setCreateTime(now);
        personExperience.setUpdateTime(now);
        return mapper.insert(personExperience) > 0 ? personExperience.getId() : 0L;
    }

    @Override
    public boolean update(PersonExperience personExperience) {
        Date now = new Date();
        personExperience.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personExperience) != 0;
    }

    @Override
    public Integer count(PersonExperienceSearchVo personExperienceSearchVo) {
        return mapper.count(personExperienceSearchVo);
    }

    @Override
    public List<PersonExperienceVo> selectList(PersonExperienceSearchVo personExperienceSearchVo) {
        return mapper.selectList(personExperienceSearchVo);
    }

    @Override
    public Page<PersonExperienceVo> selectPage(PersonExperienceSearchVo personExperienceSearchVo) {
        //先直接查总数
        int count = count(personExperienceSearchVo);
        //分页
        Page<PersonExperienceVo> page = new Page<>(true, count, personExperienceSearchVo.getPageNum(), personExperienceSearchVo.getPageSize());
        personExperienceSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonExperienceVo> responseVos = this.selectList(personExperienceSearchVo);
        page.setData(responseVos);
        return page;
    }

}